export function handleTreeData(data, targetKeys = []) {
  data.forEach((item) => {
    item["disabled"] = targetKeys.includes(item.key);
    if (item.children) {
      handleTreeData(item.children, targetKeys);
    }
  });
  return data;
}

export function findAllKeys(node) {
  let tempKeys = [];
  tempKeys.push(node.dataRef.key);
  if (node.$children && node.$children.length > 0) {
    for (let i = 0; i < node.$children.length; i++) {
      const nodeChild = node.$children[i];
      if (nodeChild.dataRef && nodeChild.dataRef.key) {
        tempKeys = [].concat(tempKeys, findAllKeys(nodeChild));
      }
    }
  }
  return tempKeys;
}

export function checkOrUnCheck(checkedKeys, keys, isChecked) {
  const keysOld = [...checkedKeys];
  let checked = [];
  const unchecked = [];
  if (isChecked) {
    checked = Array.from(new Set([...keysOld, ...keys]));
  } else {
    for (let i = 0; i < keysOld.length; i++) {
      if (keys.includes(keysOld[i])) {
        unchecked.push(keysOld[i]);
      } else {
        checked.push(keysOld[i]);
      }
    }
  }
  return {
    checked,
    unchecked
  };
}

// 递归替换字段
export function addTreeSlot(data) {
  if (!data) {
    return [];
  }
  data.forEach((el) => {
    if (el.type === "site") {
      el.disabled = true;
      el.key = "-1";
    } else {
      el.key = el.id;
    }
    el.title = el.name;

    addTreeSlot(el.companyList);
  });
  return data;
}
